@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
set v=241028
set n=Hyper-vTool
title %n%--%v%.xish
color 0A
goto up


:SET1
rem **********************************************************************************
rem *******************************这里修改参数*****************************************
rem CPU核心数量 1-4
set Count=4

rem 内存 1024MB  可以修改，单位是MB、GB
set VMmemory=4GB

rem 指定代数 1 or 2
set Generation=2

rem 网卡 默认 Default Switch    ×××修改为已有的×××
set VMswitch=Default Switch

rem 新建vhdx虚拟磁盘的大小
set NewVHDSizeBytes=256GB

rem 设置脚本延时
set time=timeout /t 5



rem **********************************************************************************
goto :eof
rem **********************************************************************************


:A
call :help
pushd %~dp0
call :SET1
call :SET2
%U%
%col%
%E1%&%E3%%n%--!v!&%E4%&%E2%&%E5%&%E2%
call:ISO
%E2%&%E6%&%E2%
call:VHDPath
%E2%&%E2%

:B
%E2%&%E7%&%E2%%T1%&%E2%&%E2%%T2%&%E2%&%E2%%T3%&%E2%&%E2%%T4%&%E2%&%E2%%TX%&%E2%&%E2%%TU%&%E7%
choice /c 1234XU !T0!:
if errorlevel 6 goto update
if errorlevel 5 del on.ps1 &exit
if errorlevel 4 goto Type
if errorlevel 3 goto Stop
if errorlevel 2 goto Link
if errorlevel 1 goto New

:New
call :OUT
(echo New &echo %time%)>>on.ps1
%T8%&%T7%

:Link
call :OUT
(echo Link &echo %time%)>>on.ps1
%T8%&%T7%

:Stop
call :OUT
(echo Stop &echo %time%)>>on.ps1
%T8%&%T7%

:Type
cls
call :OUT
type on.ps1&pause>nul &goto A

:SET2
rem 虚拟机名称-默认为当前文件夹名称
set VMName=%~dp0
set VMName=%VMName:\= %
for %%n in (%VMName%) do set VMName=%%n
rem 新建vhdx虚拟磁盘
set "NewVHDPath=%~dp0%VMName%-L1.vhdx"
rem 新建vhdx虚拟磁盘的大小
set "NewVHDSizeBytes=%NewVHDSizeBytes%"
set "VMPath=%~dp0"
set "r2=rem"
set "error=输入错误"
set "U=if /i %v% equ %Newv% (set col=color 0A) else (set col=color 0C)"

set "E1=cls & echo. & echo."
set "E2=echo.     "
set "E3=echo.                                  "
set "E4=echo.                                  -------------------"
set "E5=echo.     ----------------------------------ISO----------------------------------"
set "E6=echo.     ----------------------------------VHDX---------------------------------"
set "E7=echo.     -----------------------------------------------------------------------"

set "T0=/n /m >>>>>请输入选择"
set "T1=1.新建虚拟机"
set "T2=2.连接虚拟机"
set "T3=3.停止虚拟机并删除"
set "T4=4.查看脚本文件"
set "TX=X.退出"
set "TU=U.更新 --有更新字体会变红色"
set "T7=cls&goto B"
set "T8=PowerShell.exe -ExecutionPolicy Bypass -File on.ps1"
set T9=
set T10=
goto :eof


:ISO
rem ISO镜像
set /a a=0
if EXIST *.iso (set r1=
for /f "usebackq delims=" %%i in (`dir /b/a-d *.iso`) do (set /a a+=1
                                                          set isos[!a!]=%%~dpnxi
                                                          echo.       !a!. %%~dpnxi)
if !a! EQU 1 set VMiso=!isos[1]!
if !a! GTR 1 ( set /p t=请选择:
if !t! LSS 1   echo %error% &goto ISO
if !t! GTR !a! echo %error% &goto ISO
for /l %%n in (1,1,!t!) do (set VMiso=!isos[%%n]!))) else set r1=rem &echo.        未搜索到ISO镜像
goto :eof


:VHDPath
rem 虚拟磁盘
set /a b=0
if EXIST *.vhdx (set r3=
for /f "usebackq delims=" %%i in (`dir /b/a-d *.vhdx`) do (set /a b+=1
                                                          set vdisk[!b!]=%%~dpnxi
                                                          echo.       !b!. %%~dpnxi)
if !b! EQU 1 set VHDPath=!vdisk[1]!
if !b! GTR 1 ( set /p t=请选择:
if !t! LSS 1   echo %error% &goto VHDPath
if !t! GTR !b! echo %error% &goto VHDPath
for /l %%n in (1,1,!t!) do (set VHDPath=!vdisk[%%n]!))) else (
if EXIST *.iso (echo.        未搜索到vhdx虚拟磁盘
				echo.        
				echo.        新建：%NewVHDPath%
				echo.        大小：%NewVHDSizeBytes%
				set r2= &set r3=rem) else echo.        找不到任何镜像文件&echo.&echo.        按任意键退出& pause >nul &exit)
goto :eof

:root
echo if ^(-Not ^([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent^(^)^).IsInRole^([Security.Principal.WindowsBuiltInRole]::Administrator^)^) {
echo     if ^([int]^(Get-CimInstance -Class Win32_OperatingSystem ^| Select-Object -ExpandProperty BuildNumber^) -ge 6000^) {
echo         Start-Process PowerShell -Verb RunAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$pwd'; ^& '$PSCommandPath';`"";
echo         Exit;
echo     }
echo }
echo.
goto :eof


:OUT
rem 输出到on.ps1
(
echo.chcp 65001
echo.cls
call:root
echo.function New {
echo.	# 虚拟机参数
echo.	$VM = @{
echo.		Name = "%VMName%"
echo.		MemoryStartupBytes = %VMmemory%
echo.		Generation = %Generation%
%r2% echo.		NewVHDPath = "%NewVHDPath%"
%r2% echo.		NewVHDSizeBytes = %NewVHDSizeBytes%
echo.		BootDevice = "VHD"
%r3% echo.		VHDPath = "!VHDPath!"
%r2% echo.		Path = "%VMPath%"
echo.		SwitchName = "%VMswitch%"
echo.	}
echo.
echo.	# 新建虚拟机
echo.	New-VM @VM
echo.	# 设置CPU核心数量
echo.	Set-VMProcessor -VMName %VMName% -Count %Count%
echo.	# 关闭动态内存
echo.	Set-VMMemory -VMName %VMName% -DynamicMemoryEnabled $false
echo.	# 关闭安全启动
echo.	Set-VMFirmware -VMName %VMName% -EnableSecureBoot off
echo.	# 关闭检查点
echo.	Set-VM -Name %VMName% -CheckPointType Disable 
echo.	# 连接 虚拟机
echo.	VMconnect.exe ^(Get-VMHost^).Name %VMName%
%r1% echo.	# 添加ISO镜像
%r1% echo.	Add-VMDvdDrive -VMName %VMName% -Path "!VMiso!"
%r1% echo.	$DVDDrive = Get-VMDvdDrive -VMName %VMName%
%r1% echo.	Set-VMFirmware -VMName %VMName% -FirstBootDevice $DVDDrive
echo.}
echo.
echo.function Link {
echo.	# 连接 虚拟机
echo.	VMconnect.exe ^(Get-VMHost^).Name %VMName%
echo.}
echo.
echo.function Stop {
echo.	# 关闭 虚拟机
echo.	Stop-VM -Name %VMName% -Force
echo.	# 删除 虚拟机
echo.	Remove-VM -Name %VMName% -Force
echo.}
echo.)>on.ps1

goto :eof

rem ---备忘录---
:up
Set name=%~nx0
Set url="https://gitee.com/xish-cc/mw.config/raw/master/cmd/Hyper-vTool.cmd"
for /f "skip=3 tokens=2 delims= " %%i in ('curl -s %url%') do set New%%i &goto A
goto A

:update
rem 更新此脚本
curl -s -o %name% %url%
cls
echo.
echo.        更新完成
echo.
pause >nul
start %name%
exit

:help
set "H0=echo.		"
set "H1=echo.		当前脚本只适合 Hyper-v 虚拟机"
set "H2=echo.		下载到D盘,复制下面一行到终端运行 cmd 或者 PowerShell"
set "H3=echo.		curl -o Hyper-vTool.cmd https://gitee.com/xish-cc/mw.config/raw/master/cmd/Hyper-vTool.cmd"
set "H4=echo.		双击运行会在当前目录搜索 ISO镜像 和 vhdx虚拟磁盘 然后创建on.ps1脚本"
set "H5=echo.		当前目录只有 ISO镜像		---新建虚拟机，添加新vhdx虚拟磁盘，以ISO镜像引导启动  "
set "H6=echo.		当前目录只有 vhdx镜像		---新建虚拟机，添加已有的vhdx虚拟磁盘，以vhdx虚拟磁盘引导启动"
set "H7=echo.		当前目录 ISO和 vhdx镜像都有	---新建虚拟机，添加已有的vhdx虚拟磁盘，以ISO镜像引导启动"
set "H8=echo.		脚本开源免费使用，如需调整功能请自行研究学习"
set "H9=echo.		如果操作不当，可能造成vhdx虚拟磁盘资料丢失，注意备份，如有异常请自行负责"
set "H10=echo.		有重要资料！！！注意备份！！！"



%H0%
%H1%&%H1%&%H1%
%H0%
%H2%
%H0%
%H3%
%H0%
%H4%
%H0%
%H5%
%H6%
%H7%
%H0%
%H8%
%H9%
%H0%
%H10%&%H10%&%H10%
%H0%
%H0%按任意键继续
pause>nul
goto :eof

